///////////////////////////////////////////////////////////////////////////////
//-----------------------------------------------------------------------------
#include <Tests.h>
//-----------------------------------------------------------------------------

void matrixTest()
{
	Matrix4x4 mat1;
	f32 m[4][4];
	glMatrixMode( GL_MODELVIEW );


	//simple translate
	mat1.SetIdentity();
	glLoadIdentity();
	//
	mat1.Translate(1,2,3);
	glTranslatef(1,2,3);
	//
	glGetFloatv( GL_MODELVIEW_MATRIX, (float*)m );
	mat1 = mat1.GetGLMatrix();
	ASSERT( !memcmp( mat1.m, m, sizeof( m ) ) );


	//aditive translate
	mat1.SetIdentity();
	glLoadIdentity();
	//
	mat1.Translate(1,2,3);
	mat1.Translate(5,7,9);
	glTranslatef(1,2,3);
	glTranslatef(5,7,9);
	//
	glGetFloatv( GL_MODELVIEW_MATRIX, (float*)m );
	mat1 = mat1.GetGLMatrix();
	ASSERT( !memcmp( mat1.m, m, sizeof( m ) ) );


	//simple scale
	mat1.SetIdentity();
	glLoadIdentity();
	//
	mat1.Scale(1,2,3);
	glScalef(1,2,3);
	//
	glGetFloatv( GL_MODELVIEW_MATRIX, (float*)m );
	mat1 = mat1.GetGLMatrix();
	ASSERT( !memcmp( mat1.m, m, sizeof( m ) ) );


	//aditive scale
	mat1.SetIdentity();
	glLoadIdentity();
	//
	mat1.Scale(1,2,3);
	mat1.Scale(5,7,9);
	glScalef(1,2,3);
	glScalef(5,7,9);
	//
	glGetFloatv( GL_MODELVIEW_MATRIX, (float*)m );
	mat1 = mat1.GetGLMatrix();
	ASSERT( !memcmp( mat1.m, m, sizeof( m ) ) );


	//simple rotate
	mat1.SetIdentity();
	glLoadIdentity();
	//
	mat1.Rotate(45,1,2,3);
	glRotatef(45,1,2,3);
	//
	glGetFloatv( GL_MODELVIEW_MATRIX, (float*)m );
	mat1 = mat1.GetGLMatrix();
	ASSERT( !memcmp( mat1.m, m, sizeof( m ) ) );


	//aditive rotate
	mat1.SetIdentity();
	glLoadIdentity();
	//
	mat1.Rotate(45,1,2,3);
	mat1.Rotate(45,5,7,9);
	glRotatef(45,1,2,3);
	glRotatef(45,5,7,9);
	//
	glGetFloatv( GL_MODELVIEW_MATRIX, (float*)m );
	mat1 = mat1.GetGLMatrix();
	ASSERT( !memcmp( mat1.m, m, sizeof( m ) ) );


	//simple trs
	mat1.SetIdentity();
	glLoadIdentity();
	//
	mat1.Translate(1,2,3);
	mat1.Scale(1,2,3);
	mat1.Rotate(45,1,2,3);
	glTranslatef(1,2,3);
	glScalef(1,2,3);
	glRotatef(45,1,2,3);
	//
	glGetFloatv( GL_MODELVIEW_MATRIX, (float*)m );
	mat1 = mat1.GetGLMatrix();
	ASSERT( !memcmp( mat1.m, m, sizeof( m ) ) );


	//aditive trs
	mat1.SetIdentity();
	glLoadIdentity();
	//
	mat1.Translate(1,2,3);
	mat1.Scale(1,2,3);
	mat1.Rotate(45,1,2,3);
	mat1.Translate(5,7,9);
	mat1.Scale(5,7,9);
	mat1.Rotate(45,5,7,9);
	glTranslatef(1,2,3);
	glGetFloatv( GL_MODELVIEW_MATRIX, (float*)m );
	glScalef(1,2,3);
	glGetFloatv( GL_MODELVIEW_MATRIX, (float*)m );
	glRotatef(45,1,2,3);
	glGetFloatv( GL_MODELVIEW_MATRIX, (float*)m );
	glTranslatef(5,7,9);
	glGetFloatv( GL_MODELVIEW_MATRIX, (float*)m );
	glScalef(5,7,9);
	glGetFloatv( GL_MODELVIEW_MATRIX, (float*)m );
	glRotatef(45,5,7,9);
	//
	glGetFloatv( GL_MODELVIEW_MATRIX, (float*)m );
	mat1 = mat1.GetGLMatrix();
	ASSERT( !memcmp( mat1.m, m, sizeof( m ) ) );


	//rst
	mat1.SetIdentity();
	glLoadIdentity();
	//
	mat1.Scale(1,2,3);
	mat1.Rotate(45,1,2,3);
	mat1.Translate(1,2,3);
	glScalef(1,2,3);
	glRotatef(45,1,2,3);
	glTranslatef(1,2,3);
	//
	glGetFloatv( GL_MODELVIEW_MATRIX, (float*)m );
	mat1 = mat1.GetGLMatrix();
	ASSERT( !memcmp( mat1.m, m, sizeof( m ) ) );


	//mat1.SetIdentity();
	//glLoadIdentity();
	////
	//mat1.Translate( -1.5f, 1, 0 );
	//mat1.Rotate( 45, 0,1,0 );
	//mat1.Scale( 0.2f, 0.2f, 0.2f );
	//glTranslatef( -1.5f, 1, 0 );
	//glRotatef( 45, 0,1,0 );
	//glScalef( 0.2f, 0.2f, 0.2f );
	////
	//glGetFloatv( GL_MODELVIEW_MATRIX, (float*)m );
	//mat1 = mat1.GetGLMatrix();
	//ASSERT( !memcmp( mat1.m, m, sizeof( m ) ) );


	glMatrixMode( GL_PROJECTION );

	mat1.SetIdentity();
	glLoadIdentity();
	//
	mat1.SetPerspective(60,1,0.1f,100);
	gluPerspective(60,1,0.1f,100);
	//
	glGetFloatv( GL_PROJECTION_MATRIX, (float*)m );
	mat1 = mat1.GetGLMatrix();
	ASSERT( !memcmp( mat1.m, m, sizeof( m ) ) );

	mat1.SetIdentity();
	glLoadIdentity();
	//
	mat1.SetFrustum(-1,2,-3,4,0.1f,100);
	glFrustum(-1,2,-3,4,0.1f,100);
	//
	glGetFloatv( GL_PROJECTION_MATRIX, (float*)m );
	ASSERT( !memcmp( mat1.m, m, sizeof( m ) ) );

	mat1.SetIdentity();
	glLoadIdentity();
	//
	mat1.SetOrtho(1,2,300,400,0.1f,100);
	glOrtho(1,2,300,400,0.1f,100);
	//
	glGetFloatv( GL_PROJECTION_MATRIX, (float*)m );
	ASSERT( !memcmp( mat1.m, m, sizeof( m ) ) );

}

//-----------------------------------------------------------------------------

void vbiTest(SceneManager* scene)
{
	VertexBuffer* vbp = MGL_NEW VertexBuffer();
	vbp->AddComponent( eVDT_POSITION, eVCT_FLOAT, sizeof( f32 ), 3 );
	vbp->AddComponent( eVDT_TEXCOORD, eVCT_FLOAT, sizeof( f32 ), 2 );
	vbp->InitData( 4 );
	f32* vbpData = (f32*) vbp->GetDataPtr();

	vbpData[0] = 0.0f;
	vbpData[1] = 0.0f;
	vbpData[2] = 0.0f;
	vbpData[3] = 0.0f;
	vbpData[4] = 0.0f;
	vbpData += 5;
	vbpData[0] = 1.0f;
	vbpData[1] = 0.0f;
	vbpData[2] = 0.0f;
	vbpData[3] = 1.0f;
	vbpData[4] = 0.0f;
	vbpData += 5;
	vbpData[0] = 1.0f;
	vbpData[1] = 1.0f;
	vbpData[2] = 0.0f;
	vbpData[3] = 1.0f;
	vbpData[4] = 1.0f;
	vbpData += 5;
	vbpData[0] = 0.0f;
	vbpData[1] = 1.0f;
	vbpData[2] = 0.0f;
	vbpData[3] = 0.0f;
	vbpData[4] = 1.0f;

	VertexIndexBuffer* vibp = MGL_NEW VertexIndexBuffer();
	vibp->InitData( eIDT_U_BYTE, 6 );
	u8* vibpData = (u8*) vibp->GetDataPtr();

	vibpData[0] = 0;
	vibpData[1] = 1;
	vibpData[2] = 2;
	vibpData[3] = 0;
	vibpData[4] = 2;
	vibpData[5] = 3;

	MeshBuffers mb;
	mb.indexBuffer = vibp;
	mb.vertexBuffer = vbp;

	MeshData *m = MGL_NEW MeshData( "mymesh" );
	m->AddMeshBuffers( mb );
	m->generateBoundingSphere();
	Mesh* mesh = MGL_NEW Mesh( "myMesh" );
	mesh->SetMeshData( m );
	mesh->SetMaterial( MaterialManager::GetInstance()->GetMaterial( "Model.ms3d" ) );
	mesh->generateBoundingSphere();

	Object *obj = MGL_NEW Object();
	obj->AddMesh( mesh );
	obj->setPosition( 0.0f, -1.0f, 0.0f );

	scene->GetRootNode()->AddChild( obj );
}

//-----------------------------------------------------------------------------

#include <GarbageCollector/RefCounted.h>
#include <GarbageCollector/mgl_pointer.h>
//void* operator new( size_t size )
//{
//	return malloc(size);
//}
//
//void operator delete( void* p )
//{
//	free(p);
//}

class X	: public RefCounted
{
public:
	X(){}
};

typedef	mgl_pointer<X> XPtr;

void refTests()
{
	{
		XPtr v = new X();
	}
}

//-----------------------------------------------------------------------------
///////////////////////////////////////////////////////////////////////////////
